<?php

namespace App\Admin\Controllers;

use App\Models\Report;

use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use App\Models\Squad;
use http\Env\Response;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use DB;
use App\Admin\Extensions\Tools\DeleteReport;
use App\Api\Controllers\GetSquadController;


use App\Models\Class_section_group;
use App\Admin\Extensions\DownloadReport;


class ReportListController extends Controller
{
    use ModelForm;
    private $class;


    public function __construct(Report $report)
    {
        $this->class = $report;
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('报表列表'));
            $content->description(trans('列表'));

            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {

    }

    public function show($id,Content $content)
    {
        $content->header('报表详情');
        $content->description('详情');
        $content->row($this->show_form($id));
        $content->row($this->grid(2,$id)->render());
        return $content;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('配置报表');
            $content->description(trans('admin.create'));

            $content->body($this->create_form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($action = 1,$id = 0)
    {
        return Admin::grid(Report::class, function (Grid $grid) use ($action,$id) {
            if($action == 1){
                $user = Admin::user()->id;
                $grid->model()->where('pid','=',0)->where('operator','=',$user);
                $grid->orderBy('id','desc');
                $grid->id(trans('admin.ID'))->sortable();
                $grid->year('学年');
                $grid->month('月份');
                $grid->name('名称');
                $grid->type('类型')->display(function($type){
                    $type = explode(',',$type);
                    $string = '';
                    foreach($type as $v){
                        if ($v==0) {
                            $string .= 'N/A-';
                        }elseif($v == 1){
                            $string .= '考勤-';
                        }elseif($v == 2){
                            $string .= '听写-';
                        }elseif($v == 3){
                            $string .= '测验-';
                        }elseif($v == 4){
                            $string .= '家校-';
                        }elseif($v == 5){
                            $string .= '作业-';
                        }
                    }
                    return substr($string,0,strlen($string)-1); ;
                });
                $grid->start_time('开始时间');
                $grid->end_time('结束时间');

                $grid->column('导出进度')->display(function(){
                    return Report::where('pid','=',$this->id)->where('status','=',1)->count() . '/' . Report::where('pid','=',$this->id)->count();
                });

                $grid->actions(function ($actions) {
                    $actions->disableEdit();
                    $actions->disableDelete();
                    //$actions->append(new DownloadReport($actions->getKey()));
                });
                //禁用导出按钮
                $grid->disableExport();

                $grid->tools(function ($tools) {
                    //关闭批量删除
                    $tools->batch(function ($batch) {
                        $batch->disableDelete();
                        $batch->add('删除',new DeleteReport());
                    });
                });

                //筛选弹出框字段
                $grid->filter(function ($filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->equal('year','年度');
                    $filter->equal('month','月份');
                    $filter->equal('type','类型')->select(['1'=>'考勤',
                        '2'=>'听写',
                        '3'=>'测验',
                        '4'=>'家校',
                        '5'=>'作业']);
                    $filter->like('name','名称');
                    $filter->between('start_time','开始时间')->date();
                    
                });
            }else{
                $grid->model()->where('pid','=',$id);
                $grid->id(trans('admin.ID'))->sortable();
                $grid->name('名称');

                $grid->status('导出状态')->display(function(){
                    if($this->status == 1){
                        return '是';
                    }elseif($this->status == 0){
                        return '';
                    }
                });

                $grid->actions(function ($actions) {
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();
                    $actions->append(new DownloadReport($actions->getKey()));
                });
                //禁用导出按钮
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

/*                $grid->tools(function ($tools) {
                    //关闭批量删除
                    $tools->batch(function ($batch) {
                        $batch->disableDelete();
                    });
                });*/

                //筛选弹出框字段
                $grid->filter(function ($filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();

                    $filter->like('name','名称');
                });
            }
        });
    }

    protected function create_form()
    {
        return Admin::form(Squad::class,function (Form $form) {
            $form->setTitle('添加');
                $id = Admin::user()->id;
                $form->select('section', trans('admin.section'))->options(function(){
                    return Class_section_group::regulate_options();
                })->attribute(['id'=>'section'])->Myload('group', '/api/get_group_user',$id)->rules('required')->addElementLabelClass('must-input');
            $form->select('group',trans('admin.group'))->options(function ($id){
                    return Class_section_group::options($id);
                })->attribute(['id'=>'group'])->Myload('squad','/api/get_squads_user',$id)->rules('required')->addElementLabelClass('must-input');
            $form->select('squad',trans('admin.squad'))->attribute(['id'=>'squad'])->rules('required')->addElementLabelClass('must-input');

            $form->multipleSelect('type','类型')->options([
                    '1'=>'考勤',
                    '2'=>'听写',
                    '3'=>'测验',
                    '4'=>'家校',
                    '5'=>'作业'
                ])->rules('required')->addElementLabelClass('must-input');
                //$row->width(4)->text('start_time','开始时间(包含)')->attribute(['id'=>'startTime']);
                //$row->width(4)->text('end_time','结束时间(包含)')->attribute(['id'=>'endTime']);
            $form->date('start_time','开始时间(包含)')->attribute(['id'=>'startTime'])->rules('required')->addElementLabelClass('must-input');
            $form->date('end_time','结束时间(包含)')->attribute(['id'=>'endTime'])->rules('required')->addElementLabelClass('must-input');
            $form->setAction('submit');
        });
    }

    protected function show_form($id)
    {
        return Admin::form(Squad::class,function (Form $form) use ($id) {
            $form->setTitle('查看');
            $report = Report::where('id',$id)->get()->toArray();
            $form->row( function ($row) use ($form,$report)
            {

                $row->width(4)->select('section','班段')->options(function() use ($report){
                    return Class_section_group::where('id',$report[0]['section'])->pluck('name','id');
                })->attribute(['id'=>'section','disabled'=>'true'])->default($report[0]['section']);
                $row->width(4)->select('group',trans('admin.group'))->options(function() use ($report){
                    return Class_section_group::where('id',$report[0]['group'])->pluck('name','id');
                })->attribute(['id'=>'group','disabled'=>'true'])->default($report[0]['group']);
                $row->width(4)->select('squad','班级')->options(function() use ($report){
                    return Squad::where('id',$report[0]['squad'])->pluck('name','id');
                })->attribute(['id'=>'squad','disabled'=>'true'])->default($report[0]['squad']);
            },  $form);


            $form->row(function ($row) use ($form,$report)
            {
                $status = array('1'=>'考勤',
                    '2'=>'听写',
                    '3'=>'测验',
                    '4'=>'家校',
                    '5'=>'作业');
                $row->width(4)->multipleSelect('type','类型')->options($status)
                    ->attribute(['id'=>'type','disabled'=>'true'])->default(explode(',',$report[0]['type']));
                $row->width(4)->text('开始时间')->default($report[0]['start_time'])->attribute(['id'=>'start_time','disabled'=>'true']);
                $row->width(4)->text('结束时间')->default($report[0]['end_time'])->attribute(['id'=>'end_time','disabled'=>'true']);
            },  $form);

            $form->disableSubmit();
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
            return $form;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Squad::class,function (Form $form) {
            $form->setTitle('查看');
            $form->row(function ($row) use ($form)
            {
                $id = Admin::user()->id;
                $row->width(4)->select('section', trans('admin.section'))->options(function(){
                    return Class_section_group::regulate_options();
                })->attribute(['id'=>'section'])->Myload('group', '/api/get_group_user',$id)->rules('required');
                $row->width(4)->select('group',trans('admin.group'))->options(function ($id){
                    return Class_section_group::options($id);
                })->attribute(['id'=>'group'])->Myload('squad','/api/get_squads_user',$id)->rules('required');
                $row->width(4)->select('squad',trans('admin.squad'))->attribute(['id'=>'squad'])->load('student','/api/get_students')->rules('required');
            },  $form);

            $form->row(function ($row) use ($form)
            {
                $row->width(4)->select('student','学生id-姓名')->attribute(['id'=>'student'])->rules('required');
                //$row->width(4)->text('start_time','开始时间(包含)')->attribute(['id'=>'startTime']);
                //$row->width(4)->text('end_time','结束时间(包含)')->attribute(['id'=>'endTime']);
                $row->width(4)->date('start_time','开始时间(包含)')->attribute(['id'=>'startTime']);
                $row->width(4)->date('end_time','结束时间(包含)')->attribute(['id'=>'endTime']);
            },  $form);

            $form->row(function ($row) use ($form)
            {
                $row->width(5)->html('<div></div>');
                $row->width(3)->html('<div></div><button style="margin-left:65px;" type="button" id="btn_pdf" class="btn btn-danger">导出PDF</button>');
            },  $form);

            $form->disableSubmit();
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }

    protected function list()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('报表列表'));
            $content->description(trans('列表'));

            $content->body($this->grid()->render());
        });
    }

    protected function view()
    {

    }


    public function index_new(Request $request,Content $content)
    {
        $content->header('报表详情');
        $content->description('详情');
        $content->row($this->new_form($request));
        $content->row(view('admin.new_test'));
        return $content;
    }

    protected function new_form($request)
    {
        return Admin::form(Squad::class,function (Form $form) use ($request) {
            $form->setTitle('查看');
            $report = Report::where('id',$request['id'])->get()->toArray();
            $parent = Report::where('id',$report[0]['pid'])->get()->toArray();
            $report[0]['section'] = $parent[0]['section'];
            $report[0]['group'] = $parent[0]['group'];
            $report[0]['squad'] = $parent[0]['squad'];
            $report[0]['start_time'] = $parent[0]['start_time'];
            $report[0]['end_time'] = $parent[0]['end_time'];
            $report[0]['type'] = $parent[0]['type'];
            $form->row( function ($row) use ($form,$report)
            {

                $row->width(4)->select('section','班段')->options(function() use ($report){
                    return Class_section_group::where('id',$report[0]['section'])->pluck('name','id');
                })->attribute(['id'=>'section','disabled'=>'true'])->default($report[0]['section']);
                $row->width(4)->select('group',trans('admin.group'))->options(function() use ($report){
                    return Class_section_group::where('id',$report[0]['group'])->pluck('name','id');
                })->attribute(['id'=>'group','disabled'=>'true'])->default($report[0]['group']);
                $row->width(4)->select('squad','班级')->options(function() use ($report){
                    return Squad::where('id',$report[0]['squad'])->pluck('name','id');
                })->attribute(['id'=>'squad','disabled'=>'true'])->default($report[0]['squad']);
            },  $form);


            $form->row(function ($row) use ($form,$report)
            {
                $name = explode('-',$report[0]['name']);
                $row->width(4)->select('student','学生id-姓名')->options([$report[0]['student']=>$name[0].'-'.$name[1]])
                    ->attribute(['id'=>'student','disabled'=>'true'])->default($report[0]['student']);
                $row->width(4)->text('开始时间')->default($report[0]['start_time'])->attribute(['id'=>'start_time','disabled'=>'true']);
                $row->width(4)->text('结束时间')->default($report[0]['end_time'])->attribute(['id'=>'end_time','disabled'=>'true']);
            },  $form);

            $form->row(function ($row) use ($form,$report)
            {
                $status = array();
/*                $status[1] = array('1'=>'听写');
                $status[2] = array('2'=>'测验');
                $status[3] = array('3'=>'考勤');
                $status[4] = array('4'=>'家校');
                $status[5] = array('5'=>'作业');*/
                $status = array('1'=>'考勤',
                    '2'=>'听写',
                    '3'=>'测验',
                    '4'=>'家校',
                    '5'=>'作业');
                $row->width(4)->multipleSelect('type','类型')->options($status)
                    ->attribute(['id'=>'type','disabled'=>'true'])->default(explode(',',$report[0]['type']));
                $row->width(4)->html('<div></div>');
                $row->width(4)->html('<div></div><button style="margin-left:15px;" type="button" id="btn_pdf" class="btn btn-danger">导出PDF</button>');
            },  $form);

            $form->disableSubmit();
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }


    public function submit(Request $request){
        $students = DB::table('students')->where('squad_id',$request['squad'])->get(['en_name','id','no','ch_name']);

        $type = array('1'=>'考勤',
            '2'=>'听写',
            '3'=>'测验',
            '4'=>'家校',
            '5'=>'作业');
        $insert = array();

        $name = '';
        foreach($request['type'] as $v){
            if(!is_null($v)){
                $name .= $type[$v];
            }
        }
        $insert_type = join(',',$request['type']);
        $insert_type = substr($insert_type,0,strlen($insert_type)-1);

        $squads = DB::table('squads')->where('id',$request['squad'])->get(['name']);
        foreach($squads as $v){
            $insert = array(
                'pid'=>0,
                'name'=>$v->name.'-'.$name,
                'section'=>$request['section'],
                'operator'=>Admin::user()->id,
                'squad'=>$request['squad'],
                'group'=>$request['group'],
                'type'=>$insert_type,
                'start_time'=>$request['start_time'],
                'end_time'=>$request['end_time'],
            );
        }
        $id = DB::table('reports')->insertGetId($insert);
        $insert = array();
        foreach($students as $v){
            $insert[] = array(
                'pid'=>$id,
                'student'=>$v->id,
                'name'=>$v->no.'-'.$v->en_name.'-'.$v->ch_name
            );
        }

        DB::table('reports')->insert($insert);
        return redirect('/admin/report_list');
    }

    public function delete(Request $request){
        Report::whereIn('id',$request['ids'])->delete();
        Report::whereIn('pid',$request['ids'])->delete();
        return;
    }
}
